<!--

    Copyright (C) 2015 The Gravitee team (http://gravitee.io)

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<form name="apiProxyCtrl.formApi" ng-submit="apiProxyCtrl.update(apiProxyCtrl.api)" novalidate>
  <div class="gv-forms" layout="column">
    <div class="gv-forms-header">
      <h1>Entrypoints</h1>
      <a
        ng-click="apiProxyCtrl.switchVirtualHostMode()"
        ng-if="!apiProxyCtrl.virtualHostModeEnabled"
        permission
        permission-only="'api-definition-u'"
        >Switch to virtual-hosts mode</a
      >
      <a
        ng-click="apiProxyCtrl.switchVirtualHostMode()"
        ng-if="apiProxyCtrl.virtualHostModeEnabled && apiProxyCtrl.domainRestrictions.length === 0"
        permission
        permission-only="'api-definition-u'"
        >Switch to context-path mode</a
      >
    </div>

    <div class="gv-form">
      <h2>General</h2>
      <div class="gv-form-content">
        <div layout-gt-sm="row" ng-if="!apiProxyCtrl.virtualHostModeEnabled">
          <md-input-container class="md-block" flex-gt-sm permission permission-only="'api-gateway_definition-u'">
            <label>Gateway context-path</label>
            <input
              minlength="3"
              name="contextPath"
              ng-model="apiProxyCtrl.api.proxy.virtual_hosts[0].path"
              ng-pattern="/^\/[\/.a-zA-Z0-9-_]+$/"
              required
              type="text"
            />
            <div class="hint" ng-if="apiProxyCtrl.formApi.contextPath.$valid || apiProxyCtrl.formApi.contextPath.$pristine">
              Path where API is exposed, must start with a '/' and must contain any letter, capitalize letter, number, dash or underscore.
            </div>
            <div ng-messages="apiProxyCtrl.formApi.contextPath.$error">
              <div ng-message="required">Context path is required.</div>
              <div ng-message="minlength">Context path has to be more than 3 characters long.</div>
              <div ng-message="pattern">
                Context path is not valid (must start with a '/' and must contain any letter, capitalize letter, number, dash or underscore)
              </div>
            </div>
          </md-input-container>
          <md-input-container class="md-block" flex-gt-sm permission permission-except="'api-gateway_definition-u'">
            <label>Gateway context path</label>
            <input
              disabled
              ng-model="apiProxyCtrl.api.proxy.virtual_hosts[0].path"
              ng-pattern="/^\/[\/.a-zA-Z0-9-_]+$/"
              required
              type="text"
            />
          </md-input-container>
        </div>

        <div layout-gt-sm="row" ng-if="apiProxyCtrl.virtualHostModeEnabled">
          <div flex layout="column" layout-sm="column">
            <h5 style="color: grey">
              <span>Virtual Hosts</span>
              <ng-md-icon
                aria-label="Add virtual-host"
                icon="add_circle_outline"
                ng-click="apiProxyCtrl.addVirtualHost()"
                style="fill: #b1bdc5"
                >Add virtual-host</ng-md-icon
              >
            </h5>
            <div class="ipsum" style="color: grey; margin-bottom: 30px">Use sharding tags to control where the API must be deployed.</div>

            <div layout="row" layout-align="center center" layout-sm="column" ng-repeat="vHost in apiProxyCtrl.api.proxy.virtual_hosts">
              <ng-md-icon
                aria-label="Delete virtual-host"
                icon="remove_circle_outline"
                ng-click="apiProxyCtrl.removeVirtualHost($index)"
                style="fill: #b1bdc5"
              ></ng-md-icon>

              <div flex="5" hide-sm hide-xs>
                <!-- Spacer //-->
              </div>

              <md-input-container class="md-block" flex-gt-sm>
                <md-autocomplete
                  aria-label="Listening host"
                  class="gravitee-listening-host"
                  md-autoselect="true"
                  md-clear-button="false"
                  md-input-id="vhost{{$index}}-host"
                  md-input-name="vhost{{$index}}-host"
                  md-items="computedHost in apiProxyCtrl.getHostOptions(subDomain)"
                  md-min-length="0"
                  md-search-text="subDomain"
                  md-selected-item="vHost.host"
                  md-select-on-match="true"
                  ng-blur="apiProxyCtrl.onBlur(vHost, 'vhost'+$index+'-host')"
                  ng-click="apiProxyCtrl.onFocus('vhost'+$index+'-host')"
                  ng-pattern="apiProxyCtrl.hostPattern"
                  ng-required="apiProxyCtrl.domainRestrictions.length > 0"
                  placeholder="Listening host"
                >
                  <span>{{computedHost}}</span>
                </md-autocomplete>
                <div
                  class="hint"
                  ng-if="vHost.host === undefined || vHost.host === '' || apiProxyCtrl.formApi['vhost'+$index+'-host'].$dirty"
                >
                  Host which must be set into the HTTP request to access this entrypoint.
                </div>
              </md-input-container>

              <div flex="5" hide-sm hide-xs>
                <!-- Spacer //-->
              </div>

              <md-input-container class="md-block" flex-gt-sm>
                <input
                  aria-label="Listening Path"
                  name="vhost{{$index}}-path"
                  ng-model="vHost.path"
                  ng-pattern="/^\/[\/.a-zA-Z0-9-_]*$/"
                  placeholder="Listening path"
                  required
                  type="text"
                />
                <div
                  class="hint"
                  ng-if="vHost.path === undefined || vHost.path === '' || apiProxyCtrl.formApi['vhost'+$index+'-path'].$dirty"
                >
                  Path where API is exposed, must start with a '/' and must contain any letter, capitalize letter, number, dash or
                  underscore.
                </div>
                <div ng-messages="apiProxyCtrl.formApi['vhost'+$index+'-path'].$error">
                  <div ng-message="required">Listening path is required.</div>
                  <div ng-message="pattern">
                    Listening path is not valid (must start with a '/' and must contain any letter, capitalize letter, number, dash or
                    underscore)
                  </div>
                </div>
              </md-input-container>

              <md-input-container class="md-block" flex-gt-sm>
                <md-checkbox aria-label="Override entrypoint" name="vhost{{$index}}-override" ng-model="vHost.override_entrypoint">
                  Override entrypoint
                </md-checkbox>
                <div
                  class="hint"
                  ng-if="vHost.override_entrypoint === undefined || vHost.override_entrypoint === '' || apiProxyCtrl.formApi['vhost'+$index+'-override'].$dirty"
                >
                  Useful to override entrypoint with the virtual host on the portal.
                </div>
              </md-input-container>
            </div>
          </div>
        </div>

        <div class="md-actions gravitee-api-save-button" layout="row">
          <md-button
            class="md-raised md-primary"
            ng-disabled="apiProxyCtrl.formApi.$invalid || apiProxyCtrl.formApi.$pristine"
            permission
            permission-only="'api-definition-u'"
            type="submit"
          >
            Save
          </md-button>
          <md-button
            class="md-raised"
            ng-click="apiProxyCtrl.reset()"
            ng-disabled="apiProxyCtrl.formApi.$pristine"
            permission
            permission-only="'api-definition-u'"
            type="button"
          >
            Reset
          </md-button>
        </div>
      </div>
    </div>
  </div>
</form>
